package com.mego.weblink.database.manager;

import android.database.sqlite.SQLiteException;

import com.mego.weblink.activitys.AppContext;
import com.mego.weblink.database.dbmodel.MyContacts;
import com.mego.weblink.database.greendao.MyContactsDao;

import java.util.ArrayList;

public class MyContactsCenter {

    private static MyContactsCenter instance = new MyContactsCenter();

    public static MyContactsCenter getInstance() {
        return instance;
    }

    //查找
    public MyContacts getMyContacts(String name, String phone) {
        MyContacts MyContacts = null;
        try {
            MyContactsDao myContactsDao = AppContext.getInstances().getDaoSession().getMyContactsDao();
            ArrayList<MyContacts> thmList = new ArrayList<MyContacts>();
            thmList = (ArrayList) myContactsDao.queryBuilder()
                    .where(MyContactsDao.Properties.Name.eq(name))
                    .where(MyContactsDao.Properties.Phone.eq(phone))
                    .build().list();
            if (thmList.size() > 0) {
                MyContacts = thmList.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return MyContacts;
    }

    public Long saveMyContacts(MyContacts myContact) {
        if (myContact == null) {
            return 0L;
        }

        long id = 0;
        try {
            MyContactsDao myContactsDao = AppContext.getInstances().getDaoSession().getMyContactsDao();
            id = myContactsDao.insertOrReplace(myContact);
            myContact.setId(id);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }

        return id;
    }

    //查询未发送成功的
    public ArrayList<MyContacts> getMyContactsNotSend(int limit) {
        if (limit == 0) {
            limit = 20;
        }

        try {
            MyContactsDao myContactsDao = AppContext.getInstances().getDaoSession().getMyContactsDao();
            ArrayList<MyContacts> myContactsList = (ArrayList) myContactsDao.queryBuilder()
                    .where(MyContactsDao.Properties.Issend.eq(false))
                    .limit(limit)
                    .build().list();
            return myContactsList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
